import React from 'react';
import { connect } from 'dva';
import { Table, Row, Col, Form, Input, Radio, DatePicker, Select, Button, Popconfirm, message } from 'antd';
const R = require('ramda');
const dateFormat = 'YYYY-MM-DD';
const { RangePicker } = DatePicker;
import RightContacts from '../../../utils/RightConstants';
import OutbuyDetail from '../../../components/outbuy/components/OutbuyDetail';
import RightBox from "../../../components/commons/RightBox";
import Constants from "../../../utils/Constants";
import EditOutbuy from '../../../components/outbuy/components/EditOutbuy';
const FormItem = Form.Item;
const RadioGroup = Radio.Group;
export default connect(({ outbuyProvider }) => ({ outbuyProvider }))(
    Form.create()(class customerDetails extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                open: false
            }
        }

        componentDidMount() {
            this.props.dispatch({ type: 'outbuyProvider/getOutbuyProviderList', payload: { status: "4", page: Constants.DEFAULT_PAGE } });
        }


        handleOpen = (type, record, disabled) => {
            var id = record && record.id ? record.id : null;
            if (id) {
                this.props.dispatch({
                    type: 'outbuyProvider/getById',
                    payload: { id }
                })
            }

            this.editOutbuy.handleOpen(type, id, disabled);
        }


        onView = (record) => {
            this.handleOpenDetail(record, false, {}, "1", true);
        }


        handleOpenDetail = (record, customerDisabled, searchConditions, tabActiveKey, editCustomerDisable) => {
            this.props.dispatch({
                type: 'outbuyProvider/getById',
                payload: {
                    id: record.id,
                    onSuccess: () => {
                        this.props.dispatch({ type: "outbuyPayer/getOutbuyPayerList", payload: {providerId: record.id, page: Constants.DEFAULT_PAGE }});
                        this.props.dispatch({type: 'outbuyProduct/getProductList', payload: { providerId: record.id, status: "1", page: Constants.DEFAULT_PAGE }});
                        this.refs.outbuyDetail.handleOpen(customerDisabled, searchConditions, tabActiveKey, record.id, editCustomerDisable, "customerDetail");
                    }
                }
            });

        }


        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.outbuyProvider.page;
            newPage.page = 1;
            let time = values.time;
            this.props.dispatch({
                type: 'outbuyProvider/getOutbuyProviderList',
                payload: {
                    ...values,
                    page: newPage
                }
            });
        };


        columns = [
            { title: '客户名称', dataIndex: 'cstName', key: 'cstName' },
            { title: '供应商编码', dataIndex: 'providerNo', key: 'providerNo' },

            { title: '供应商名称', dataIndex: 'name', key: 'name', },

            {
                title: '业务经理', render(record) {
                    if (R.isNil(record.managerName) || R.isEmpty(record.managerName)) {
                        return "未分配";
                    } else {
                        return record.managerName;
                    }
                }
            },

            {
                title: '跟单经理', render(record) {
                    if (R.isNil(record.waiterName) || R.isEmpty(record.waiterName)) {
                        return "未分配";
                    } else {
                        return record.waiterName;
                    }
                }
            },

            { title: '创建时间', dataIndex: 'createTime', key: 'createTime' },

            {
                title: '操作', key: 'key_oprator',

                render: (text, record, index) => {

                    let infoMaintenance = <a href="#" onClick={this.onView.bind(this, record)}>信息维护</a>
                    let infoEdit = <span><a href="#" onClick={this.handleOpen.bind(this, 'edit', record, false)}>编辑</a><span className="ant-divider" /></span>
                    let infoView = <span><a href="#" onClick={this.handleOpen.bind(this, 'view', record, true)}>查看</a></span>;
                    return (
                        <span>
                            {record.status === '1' || record.status === '3' ? infoEdit : ''}

                            {
                                record.status === '1' || record.status === '3' ?
                                <span>
                                    <Popconfirm placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'}
                                        onConfirm={() => {
                                            this.props.dispatch({
                                                type: 'outbuyProvider/deleteById',
                                                payload: {
                                                    id: record.id,
                                                    onSuccess: () => {
                                                        this.handleSearch();
                                                    }
                                                }
                                            })
                                        }} okText="是" cancelText="否">
                                        <a href="#" disabled={this.props.disabled}>删除</a>
                                    </Popconfirm>
                                     <span className="ant-divider" />
                                </span>

                                    : ""
                            }

                            {record.status === '4'? '' : infoView}

                            {record.status === '4' ? <span>{infoMaintenance}</span> : ''}
                        </span>
                    );
                }
            }
        ];


        render() {
            let dataSouce = R.isNil(this.props.outbuyProvider.dataSource) ? {} : this.props.outbuyProvider.dataSource;

            let formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                pageSize: this.props.outbuyProvider.page.size,
                total: this.props.outbuyProvider.page.totalRow,
                showSizeChanger: true,
                current: this.props.outbuyProvider.page.page,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.outbuyProvider.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'outbuyProvider/getOutbuyProviderList',
                        payload: {
                            ...values,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.outbuyProvider.page;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'outbuyProvider/getOutbuyProviderList',
                        payload: {
                            ...values,
                            page: newPage
                        }
                    });
                }
            }



            return (
                <div>
                    <Row>
                        <Col span={7}><FormItem {...formItemLayout} label={'供应商名称'}>{getFieldDecorator('providerName')(<Input />)}</FormItem></Col>
                        <Col span={7}><FormItem {...formItemLayout} label={'客户名称'}>{getFieldDecorator('cstName')(<Input />)}</FormItem></Col>
                    </Row>
                    <Row>
                        <Col span={7}>
                            <FormItem {...formItemLayout} label={'状态'}>
                                {getFieldDecorator('status', {
                                    initialValue: "4"
                                })(

                                    <RadioGroup style={{ width: 800 }}>
                                        <Radio value="1">待申请</Radio>
                                        <Radio value="2">待审核</Radio>
                                        <Radio value="3">待补充资料</Radio>
                                        <Radio value="4">审核通过</Radio>
                                        <Radio value="5">审核不通过</Radio>
                                    </RadioGroup>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12} style={{ float: 'right' }}>
                            <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>
                    </Row>

                    <Table rowKey="id" dataSource={this.props.outbuyProvider.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />


                    <EditOutbuy wrappedComponentRef={(inst) => this.editOutbuy = inst} dataSource={this.props.outbuyProvider.dataSource} handleSearch={this.handleSearch} />


                    <OutbuyDetail ref={"outbuyDetail"} cstId={dataSouce.id} showPA={true}
                        dispatch={this.props.dispatch}
                        {...this.props.outbuyProvider}
                        handleSearch={this.handleSearch} />

                </div>
            )
        }
    })


)
